package atmGUI;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//菜单
public class MenuPanel extends JPanel implements ActionListener {
    private Icon queryImage = Util.getIcon("/image/buttonFind.jpg");//查询按钮图
    private Icon saveImage = Util.getIcon("/image/buttonGet.jpg");//取款按钮图
    private Icon withdrawalImage = Util.getIcon("/image/buttonSave.jpg");//存款按钮图
    private Icon transferImage = Util.getIcon("/image/pane.jpg");//转账按钮图
    private Icon changePwdImage = Util.getIcon("/image/buttonModify.jpg");//改密码按钮图
    private Icon quitImage = Util.getIcon("/image/buttonOff.jpg");//退出按钮图
    private JButton queryBtn;
    private JButton saveBtn;
    private JButton withdrawalBtn;
    private JButton transferBtn;
    private JLabel transferJLabel;
    private JButton changePwdBtn;
    private JButton quitBtn;
    private ATM_GUI_Main frame;//顶级容器实例

    public MenuPanel(ATM_GUI_Main frame) {
        this.frame = frame;
        //设置空布局
        this.setLayout(null);
        this.setOpaque(false);
        //查询按钮
        queryBtn = new JButton(queryImage);
        queryBtn.setBounds(0, 220, 140, 50);
        this.add(queryBtn);
        queryBtn.addActionListener(this);//建立联系
        //取款按钮
        saveBtn = new JButton(saveImage);
        saveBtn.setBounds(0, 300, 140, 50);
        this.add(saveBtn);
        saveBtn.addActionListener(this);
        //存款按钮
        withdrawalBtn = new JButton(withdrawalImage);
        withdrawalBtn.setBounds(0, 380, 140, 50);
        this.add(withdrawalBtn);
        withdrawalBtn.addActionListener(this);
        //转账按钮
        transferJLabel = new JLabel("转  账");
        transferJLabel.setBounds(900, 220, 140, 50);
        transferJLabel.setFont(new Font("宋体", Font.BOLD, 25));
        transferJLabel.setForeground(Color.white);
        this.add(transferJLabel);
        transferBtn = new JButton(transferImage);
        transferBtn.setBounds(870, 220, 140, 50);
        transferBtn.setOpaque(false);
        this.add(transferBtn);
        transferBtn.addActionListener(this);
        //改密码按钮
        changePwdBtn = new JButton(changePwdImage);
        changePwdBtn.setBounds(870, 300, 140, 50);
        this.add(changePwdBtn);
        changePwdBtn.addActionListener(this);
        //退出按钮
        quitBtn = new JButton(quitImage);
        quitBtn.setBounds(870, 380, 140, 50);
        this.add(quitBtn);
        quitBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == queryBtn) {
            //用中转站调用查询界面，设置余额为当前用户的余额
            frame.getQueryPanel().steJLabelBalances(frame.getLoginUser().getBalance());
            frame.switchPanel("query");
        } else if (e.getSource() == withdrawalBtn) {
            frame.switchPanel("withdrawal");
        } else if (e.getSource() == saveBtn) {
            frame.switchPanel("save");
        } else if (e.getSource() == transferBtn) {
            frame.switchPanel("transfer");
        } else if (e.getSource() == changePwdBtn) {
            frame.switchPanel("changePwd");
        } else if (e.getSource() == quitBtn) {//当点击退出按钮时，切换到登陆界面
            frame.getLoginPanel().getIptPassword().setText("");
            frame.switchPanel("login");
        }
    }
}
